<?php

class Core_Validate_NotEmpty extends Zend_Validate_NotEmpty
{
	const RICHTEXT			= 2048;
	protected $_expandType	= 0;
    public function __construct($options = null)
    {
		$this->_constants = array_merge($this->_constants,array(
			self::RICHTEXT => 'richtext'			
		));	
		if( is_int($options['type']) )
		{
			$this->setExpandType($options['type'] & (~self::ALL));
			$options['type']	= $options['type'] & self::ALL;
		}
        parent::__construct($options);
    }

	protected function setExpandType($value)
	{
		$this->_expandType = $value;
	}
	protected function getExpandType()
	{
		return $this->_expandType;
	}
    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value is not an empty value.
     *
     * @param  string $value
     * @return boolean
     */
    public function isValid($value)
    {
		return parent::isValid($value) && $this->isValidExpand($value);
	}
	protected function isValidExpand($value)
	{
		$type = $this->getExpandType();
		if( $type & self::RICHTEXT )
		{
			$text = strip_tags($value);
			$text = $this->htmltrim($text);
			if( !$text )
			{
				$this->_error(self::IS_EMPTY);
				return false;
			}
		}
		return true;
	}
	function htmltrim($string)
	{
		$pattern = '(?:[ \t\n\r\x0B\x00\x{A0}\x{AD}\x{2000}-\x{200F}\x{201F}\x{202F}\x{3000}\x{FEFF}]|&nbsp;|<br\s*\/?>)+';
		return preg_replace('/^' . $pattern . '|' . $pattern . '$/u', '', $string);
	}

}
